What is randexp?
The randexp npm package is used to generate random strings that match a given regular expression. This can be useful for testing, generating mock data, or any scenario where you need random but structured strings.
What are randexp's main functionalities?
Generate Random String from Regex
This feature allows you to generate a random string that matches a given regular expression. In this example, the generated string will match the pattern 'hello+ (world|to you)'.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/);
console.log(randexp.gen());
Custom Randomness
You can customize the randomness by overriding the `randInt` method. In this example, the `randInt` method is overridden to always return the lower bound, making the output deterministic.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/);
randexp.randInt = (a, b) => a; // Always return the lower bound
console.log(randexp.gen());
Using Flags
RandExp supports regex flags such as case insensitivity. In this example, the 'i' flag makes the regex case insensitive.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/i); // Case insensitive
console.log(randexp.gen());
Other packages similar to randexp
faker
Faker is a popular library for generating fake data. While it doesn't generate strings based on regular expressions, it provides a wide range of methods for generating random data such as names, addresses, and phone numbers. It is more feature-rich in terms of the variety of data it can generate compared to randexp.
chance
Chance is another library for generating random data. It offers a variety of random data generators, including strings, numbers, and even entire objects. Like Faker, it does not focus on regex-based string generation but provides a broader range of random data generation capabilities.
xeger
Xeger is a library specifically designed to generate strings that match a given regular expression, similar to randexp. It is a JavaScript port of the Java library Xeger and offers similar functionality to randexp, focusing on regex-based string generation.
randexp.js
randexp will generate a random string that matches a given RegExp Javascript object.
Usage
const RandExp = require('randexp');
new RandExp(/hello+ (world|to you)/).gen();
new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
new RandExp(/random stuff: .+/).gen();
new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
new RandExp(new RegExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i'));
If you're only going to use gen()
once with a regexp and want slightly shorter syntax for it
const randexp = require('randexp').randexp;
randexp(/[1-6]/);
randexp('great|good( job)?|excellent');
If you miss the old syntax
require('randexp').sugar();
/yes|no|maybe|i don't know/.gen();
Motivation
Regular expressions are used in every language, every programmer is familiar with them. Regex can be used to easily express complex strings. What better way to generate a random string than with a language you can use to express the string you want?
Thanks to String-Random for giving me the idea to make this in the first place and randexp for the sweet .gen()
syntax.
Default Range
The default generated character range includes printable ASCII. In order to add or remove characters,
a defaultRange
attribute is exposed. you can subtract(from, to)
and add(from, to)
const randexp = new RandExp(/random stuff: .+/);
randexp.defaultRange.subtract(32, 126);
randexp.defaultRange.add(0, 65535);
randexp.gen();
You can also change the default range by changing RandExp.prototype.defaultRange
.
Custom PRNG
The default randomness is provided by Math.random()
. If you need to use a seedable or cryptographic PRNG, you
can override RandExp.prototype.randInt
or randexp.randInt
(where randexp
is an instance of RandExp
). randInt(from, to)
accepts an inclusive range and returns a randomly selected number within that range.
Infinite Repetitionals
Repetitional tokens such as *
, +
, and {3,}
have an infinite max range. In this case, randexp looks at its min and adds 100 to it to get a useable max value. If you want to use another int other than 100 you can change the max
property in RandExp.prototype
or the RandExp instance.
const randexp = new RandExp(/no{1,}/);
randexp.max = 1000000;
With RandExp.sugar()
const regexp = /(hi)*/;
regexp.max = 1000000;
Bad Regular Expressions
There are some regular expressions which can never match any string.
-
Ones with badly placed positionals such as /a^/
and /$c/m
. Randexp will ignore positional tokens.
-
Back references to non-existing groups like /(a)\1\2/
. Randexp will ignore those references, returning an empty string for them. If the group exists only after the reference is used such as in /\1 (hey)/
, it will too be ignored.
-
Custom negated character sets with two sets inside that cancel each other out. Example: /[^\w\W]/
. If you give this to randexp, it will return an empty string for this set since it can't match anything.
Projects based on randexp.js
JSON-Schema Faker
Use generators to populate JSON Schema samples. See: jsf on github and jsf demo page.
Install
Node.js
npm install randexp
Browser
Download the minified version from the latest release.
Tests
Tests are written with mocha
npm test
Integration with TypeScript
RandExp includes TypeScript definitions.
import * as RandExp from "randexp";
const randexp = new RandExp(/[a-z]{6}/);
randexp.gen();
Use dtslint to check the definition file.
npm install -g dtslint
npm run dtslint